package MypageController.MyComment;

import java.io.IOException;

import java.io.PrintWriter;
import org.json.simple.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import MypageController.MyMain.MyMainDAO;

/**
 * Servlet implementation class MyCommentServ
 */
public class MyCommentServ extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyCommentServ() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		request.setCharacterEncoding("UTF-8");
 		response.setCharacterEncoding("UTF-8");
 		response.setContentType("text/html; charset=UTF-8");
 		RequestDispatcher view = null;
 		
 		MyMainDAO mymaindao = new MyMainDAO();
 		
 		MyCommentDAO mycommentdao = new MyCommentDAO();
 		MyCommentBean mycommentbean = new MyCommentBean();
 		MyCommentBean[] mycomment = null;
		String action=request.getParameter("action");
		
		String id = null;
		if(request.getParameter("id") != null){
			id=request.getParameter("id");
 		}
 		
 		if(action.equals("write")){
 			System.out.println("아작스");
 			System.out.println("코맨트 내용 : " + request.getParameter("commentarea"));
 			
 			int entriesid = Integer.parseInt(request.getParameter("entriesid"));
 			mycommentbean.setBlogs_Id(Integer.parseInt(request.getParameter("mypageid")));
 			mycommentbean.setEntries_Id(entriesid);
 			mycommentbean.setCblog_Contents(request.getParameter("commentarea"));
 			mycommentbean.setCblog_Nick(id);
 			
 			mycommentdao.commentWrite(mycommentbean);
 			
 			mycomment = mycommentdao.commentGet(entriesid);

		    JSONArray inner = new JSONArray();

		    for(int i = 0 ; i < mycomment.length ; i++){
		    	JSONObject obj2 = new JSONObject();
		    	obj2.put("id", mycomment[i].getID());
		    	obj2.put("blogs_id", mycomment[i].getBlogs_Id());
		    	obj2.put("entries_id", mycomment[i].getEntries_Id());
		    	obj2.put("cblog_nick", mycomment[i].getCblog_Nick());
		    	obj2.put("cblog_date", mycomment[i].getCblog_Date());
		    	obj2.put("cblog_contents", mycomment[i].getCblog_Contents());
		    	inner.add(obj2);
		    }

		    JSONObject obj = new JSONObject();
		    
		    obj.put("conversion", inner);
		 
		    System.out.println(obj);
		    
		    PrintWriter out = response.getWriter();
		    out.println(obj);
		    out.close();

 		}else if(action.equals("delet")){
 			
 			System.out.println("아작스삭제");
 			
 			int commentid =Integer.parseInt(request.getParameter("commentid"));
 			int entriesid = Integer.parseInt(request.getParameter("entriesid"));
 			
 			mycommentdao.commentDelet(commentid);
 			
 			int result = mycommentdao.commentCountGet(entriesid);
 			
 			String resultString = null;			
 			
			resultString = "<response><result>"+result+"</result></response>";
		    PrintWriter out = response.getWriter();
		    response.setContentType("text/xml;charset=utf-8");
		   
		    out.println(resultString);
		    out.close();
 			
 			
/*******************************************************************************************************		  
		    JSONArray inner = new JSONArray();

		    for(int i = 0; i < mycomment.length; i++){
		    	JSONObject obj2 = new JSONObject();
		    	obj2.put("id", mycomment[i].getID());
		    	obj2.put("blogs_id", mycomment[i].getBlogs_Id());
		    	obj2.put("entries_id", mycomment[i].getEntries_Id());
		    	obj2.put("cblog_nick", mycomment[i].getCblog_Nick());
		    	obj2.put("cblog_date", mycomment[i].getCblog_Date());
		    	obj2.put("cblog_contents", mycomment[i].getCblog_Contents());
		    	inner.add(obj2);
		    }

		    JSONObject obj = new JSONObject();
		    
		    obj.put("conversion", inner);
		 
		    System.out.println(obj);
		    
		    PrintWriter out = response.getWriter();
		    out.println(obj);
		    out.close();
*****************************************************************************************************/
 		}else if(action.equals("view")){
 			
 			int entriesid = Integer.parseInt(request.getParameter("entriesid"));
 			
 			mycomment = mycommentdao.commentGet(entriesid);

		    JSONArray inner = new JSONArray();

		    for(int i = 0; i < mycomment.length; i++){
		    	JSONObject obj2 = new JSONObject();
		    	obj2.put("id", mycomment[i].getID());
		    	obj2.put("blogs_id", mycomment[i].getBlogs_Id());
		    	obj2.put("entries_id", mycomment[i].getEntries_Id());
		    	obj2.put("cblog_nick", mycomment[i].getCblog_Nick());
		    	obj2.put("cblog_date", mycomment[i].getCblog_Date());
		    	obj2.put("cblog_contents", mycomment[i].getCblog_Contents());
		    	inner.add(obj2);
		    }

		    JSONObject obj = new JSONObject();
		    
		    obj.put("conversion", inner);
		 
		    System.out.println(obj);
		    
		    PrintWriter out = response.getWriter();
		    out.println(obj);
		    out.close();
 			
 		}
	
	}

}
